/*
 * Copyright (c) 2011. This file is public under GPLv3 license.
 * For more information about license please refer to http://www.gnu.org/licenses/gpl.html
 */

package com.freetools.watchdog.model;

import com.freetools.watchdog.engine.DAO;
import org.apache.log4j.Logger;

import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Date: Feb 15, 2011
 *
 * @author Michael Morozov
 */
public class ModelHolder implements IModelHolder
{
    private static final Logger log = Logger.getLogger(ModelHolder.class);
    private List<IColumn> columns = new ArrayList<IColumn>();
    private List<IRecord> records = new ArrayList<IRecord>();
    private List<ICommand> commands = new ArrayList<ICommand>();
    private List<ISource> sources = new ArrayList<ISource>();
    private String parsePattern = "";
    private Writer spool = new OutputStreamWriter(System.out);
    private IRecordFormatter recordFormatter = new CommaSeparateRecordFormatter();
    private String query = DAO.DEFAULT_QUERY;
    private String dbFile = DAO.DEFAULT_DB_FILE;

    public List<IPiece> getReadPieces()
    {
        return readPieces;
    }

    public String getQuery()
    {
        return query;
    }

    public void setQuery(String query)
    {
        this.query = query;
    }

    public void copyTo(IModelHolder model)
    {
        if (model instanceof ModelHolder)
        {
            ModelHolder _model = (ModelHolder) model;
            _model.records = this.records;
            _model.columns = this.columns;
            _model.sources = this.sources;
            _model.readPieces = this.readPieces;
        }

    }

    public void dataClear()
    {
        records.clear();
        sources.clear();
        readPieces.clear();
        columns.clear();
    }

    private List<IPiece> readPieces = new ArrayList<IPiece>();

    public List<IColumn> getColumns()
    {
        return columns;
    }


    public List<ICommand> getCommands()
    {
        return commands;
    }

    public String getDBFile()
    {
        return dbFile;
    }

    public void setDBFile(String dbFile)
    {
        this.dbFile = dbFile;
    }

    public String getParsePattern()
    {
        return parsePattern;
    }

    public void setParsePattern(String parsePattern)
    {
        this.parsePattern = parsePattern;
    }

    public Writer getSpool()
    {
        return spool;
    }

    public void setSpool(Writer writer)
    {
        spool = writer;
    }

    public IRecordFormatter getRecordFormatter()
    {
        return recordFormatter;
    }

    public void setRecordFormatter(IRecordFormatter formatter)
    {
        recordFormatter = formatter;
    }

    public Collection<IRecord> getRecords()
    {
        return DAO.getRecords(this);
    }

    public List<ISource> getSources()
    {
        return sources;
    }
}
